package org.codeart.driver.controller;

import cn.hutool.core.map.MapUtil;
import org.codeart.common.dto.DriverUser;
import org.codeart.common.util.HttpStatus;
import org.codeart.common.util.R;
import org.codeart.driver.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class DriverUserController {

    @Autowired
    private DriverUserService driverUserService;

    @PostMapping("/user")
    public R<Void> add(@RequestBody DriverUser driverUser) {
        boolean b = driverUserService.addUser(driverUser);
        if (b) {
            return R.ok();
        }
        return R.fail();
    }

    @PutMapping("/user")
    public R<Void> update(@RequestBody DriverUser driverUser) {
        boolean b = driverUserService.updateUser(driverUser);
        if (b) {
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("/check-driver/{driverPhone}")
    public R<Map<Object, Object>> getUser(@PathVariable("driverPhone") String driverPhone) {
        DriverUser user = driverUserService.getUser(driverPhone);
        if (user != null) {
            Map<Object, Object> data = MapUtil.builder()
                                              .put("driverPhone", user.getDriverPhone())
                                              .put("exists", 1)
                                              .build();
            return R.ok(data);
        }
        return R.fail(HttpStatus.HTTP_NOT_FOUND, "司机信息不存在");
    }


}
